package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ja.openchat.ui.chat.group.MoreMemberActivity;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.group.SuperGroupFullInfo;
import com.zy.openchat.core.model.group.SuperGroupInfo;
import com.zy.openchat.core.model.message.ChatInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.goldze.mvvmhabit.base.BaseViewModel;


public class MoreMemberViewModel extends BaseViewModel {


    private MoreMemberActivity mcontext;
    private RecyclerView recyclerView;

    public MoreMemberViewModel(@NonNull Application application) {
        super(application);
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        switch (tg_event.event) {
            case EUser_Td_Chat_Title_Changed: {
                //会话标题修改通知
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (mcontext.chatInfo == chat) {
                    mcontext.chatInfo.title = chat.title;
                    mcontext.chatInfo.title_full_py = chat.title_full_py;
                }
            }
            break;
            case EUser_Td_Chat_Permissions_Changed: {
                //会话权限变更通知
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (mcontext.chatInfo == chat) {
                    mcontext.chatInfo.permissions = chat.permissions;
                    mcontext.refreshMembersListUI();
                }
            }
            break;
            case EUser_Td_Chat_Photo_Changed:
            case EUser_Td_Group_Photo_Ok: {
                //会话头像修改通知
                //群组头像下载完成
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (mcontext.chatInfo == chat) {
                    mcontext.chatInfo.photo = chat.photo;
                }
            }
            break;
            case EUser_Td_Contact_Photo_Ok: {
                //联系人头像更新
                mcontext.memberGridAdapter.notifyDataSetChanged();
            }
            break;
            case EUser_Td_Group_Super_Info_Changed: {
                SuperGroupInfo info = (SuperGroupInfo) tg_event.data;
                if (info.id == mcontext.chatInfo.superGroupId()) {
                    mcontext.super_groupInfo = info;
                    mcontext.refreshMembersListUI();
                }
            }
            break;
            case EUser_Td_Group_Super_Full_Info_Changed: {
                long id = (long) tg_event.dataEx;
                SuperGroupFullInfo info = (SuperGroupFullInfo) tg_event.data;
                if (id == mcontext.chatInfo.superGroupId()) {
                    //延时请求群组成员
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mcontext.super_groupFullInfo = info;
                            mcontext.refreshMembersListUI();
                            //刷新列表
                            mcontext.getSuperMembers();
                            mcontext.getSuperAdminMembers();
                        }
                    }, 100);
                }
            }
            break;
        }
    }

    public void setFragment(MoreMemberActivity moreMemberActivity, RecyclerView recyclerView) {
        this.mcontext = moreMemberActivity;
        this.recyclerView = recyclerView;
    }
}
